package com.cvnert.testplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvnert.testplatform.domain.Task;
import com.cvnert.testplatform.domain.TaskVersion;
import com.cvnert.testplatform.domain.User;
import com.cvnert.testplatform.domain.Version;
import com.cvnert.testplatform.domain.vo.TaskListVo;
import com.cvnert.testplatform.domain.vo.TaskVo;
import com.cvnert.testplatform.mapper.TaskVersionMapper;
import com.cvnert.testplatform.mapper.UserMapper;
import com.cvnert.testplatform.mapper.VersionMapper;
import com.cvnert.testplatform.service.TaskService;
import com.cvnert.testplatform.mapper.TaskMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【task】的数据库操作Service实现
* @createDate 2024-03-24 14:48:07
*/
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService{
    @Resource
    private TaskMapper taskMapper;

    @Resource
    private VersionMapper versionMapper;

    @Resource
    private TaskVersionMapper taskVersionMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Object addTask(TaskVo taskVo) {
        HashMap<String, Object> res = new HashMap<>();
        if (taskVo == null) {
            res.put("state", false);
            res.put("msg", "参数不允许为空！");
            return res;
        }
        Version version = versionMapper.selectById(taskVo.getVid());
        if (version == null) {
            res.put("state", false);
            res.put("msg", "版本不存在！");
            return res;
        }else{
            Task task = new Task();
            task.setTaskname(taskVo.getTaskname());
            task.setTaskdes(taskVo.getTaskdes());
            task.setTaskmain(taskVo.getTaskmain());
//            task.setTaskUser(taskVo.getTaskUser());
            task.setTaskuser(taskVo.getTaskuser());
            task.setTasktouser(taskVo.getTasktouser());
            task.setCreatetime(LocalDateTime.now());


            int insert = taskMapper.insert(task);
            if (insert > 0){
                res.put("state", true);
                res.put("msg", "新增成功！");
                Task task1 = taskMapper.selectById(task.getId());
                task1.setTid(task.getId());
                taskMapper.updateById(task1);
                TaskVersion taskVersion1 = new TaskVersion();
                taskVersion1.setTid(task.getId());
                taskVersion1.setVid(taskVo.getVid());
                taskVersionMapper.insert(taskVersion1);
                return res;
            }
        }

        return res;
    }

    @Override
    public Object deleteTask(Integer tid) {
        // 判断任务是否存在
        Task task1 = taskMapper.selectById(tid);
        if (task1 == null) {
            HashMap<String, Object> res = new HashMap<>();
            res.put("state", false);
            res.put("msg", "任务不存在！");
            return res;
        }
        HashMap<String, Object> res = new HashMap<>();
        Task task = taskMapper.selectById(tid);
        if (task == null) {
            res.put("state", false);
            res.put("msg", "任务不存在！");
            return res;
        }
        int i = taskMapper.deleteById(tid);
        if (i == 1) {
            res.put("state", true);
            res.put("msg", "删除成功！");
        } else {
            res.put("state", false);
            res.put("msg", "删除失败！");
        }
        return res;
    }

    @Override
    public Object updateTask(TaskVo taskVo) {
        // 判断任务是否存在
        HashMap<String, Object> res = new HashMap<>();
        Task task1 = taskMapper.selectById(taskVo.getTid());
        if (task1 == null) {
            res.put("state", false);
            res.put("msg", "版本不存在！");
            return res;
        }
        Task task = new Task();
        task.setTaskname(taskVo.getTaskname());
        task.setTaskdes(taskVo.getTaskdes());
        task.setUpdatetime(LocalDateTime.now());
        task.setTaskmain(taskVo.getTaskmain());
        task.setTaskuser(taskVo.getTaskuser());
        task.setTasktouser(taskVo.getTasktouser());
        task.setId(taskVo.getTid());
        int i = taskMapper.updateById(task);
        if (i == 1) {
            res.put("state", true);
            res.put("msg", "更新成功！");
        } else {
            res.put("state", false);
            res.put("msg", "更新失败！");
        }
        return null;
    }

    @Override
    public Object getTaskList(TaskListVo taskListVo) {
        HashMap<String, Object> res = new HashMap<>();
//        // 判断版本是否存在
        Version version = versionMapper.selectById(taskListVo.getVid());
        if (version == null) {
            res.put("state", false);
            res.put("msg", "版本不存在！");
            return res;
        }
        Page<Task> taskPage = new Page<>(taskListVo.getPageNum(),taskListVo.getPageSize());
        QueryWrapper<Task> wrapper = new QueryWrapper<>();
        if (taskListVo.getTaskname() != null && !taskListVo.getTaskname().equals("")) {
            wrapper.eq("taskName",taskListVo.getTaskname());
        }
        if(taskListVo.getTaskUser()!=null){
            ArrayList<User> objects = new ArrayList<>();
            wrapper.eq("taskUser",taskListVo.getTaskUser());
        }
        if (taskListVo.getTaskToUser() != null) {
            wrapper.eq("taskToUser",taskListVo.getTaskToUser());
        }


        ArrayList<User> users = new ArrayList<>();
        ArrayList<User> userss = new ArrayList<>();
        taskMapper.selectList(wrapper).forEach(item -> {
            if (item != null) {
                String taskUser = item.getTaskuser();
                if (taskUser != null) {
                    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                    userQueryWrapper.eq("username", taskUser);
                    User user = userMapper.selectOne(userQueryWrapper);
                    if (user != null && taskUser.equals(user.getUsername())) {
                        users.add(user);
                        item.setCreateUser(user); // 这里可能有问题，应该是设置单个用户而不是整个列表
                    }
                }
            }
        });

        // 迭代查询列表中所有的taskToUser

        taskMapper.selectList(wrapper).forEach(item -> {
            if (item != null) {
                String tasktouser = item.getTasktouser();
                if (tasktouser != null) {
                    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                    userQueryWrapper.eq("username", tasktouser);
                    User user = userMapper.selectOne(userQueryWrapper);
                    if (user != null && tasktouser.equals(user.getUsername())) {
                        userss.add(user);
                        item.setFinishUser(user); // 这里可能有问题，应该是设置单个用户而不是整个列表
                    }
                }
            }
        });

        Page<Task> taskPage1 = taskMapper.selectPage(taskPage, wrapper);

        taskPage1.getRecords().forEach(item->{
            ArrayList<User> objects = new ArrayList<>();
            ArrayList<User> objects1 = new ArrayList<>();
            users.forEach(user -> {
                if (Objects.equals(user.getUsername(), item.getTaskuser())) {
                    objects.add(user);
                    // 去重
                    List<User> collect = objects.stream().distinct().collect(Collectors.toList());
                    item.setCreateUser(collect.get(0));
                }
            });
            userss.forEach(user -> {
                if (Objects.equals(user.getUsername(), item.getTasktouser())) {
                    objects1.add(user);
                    List<User> collect = objects1.stream().distinct().collect(Collectors.toList());
                    item.setFinishUser(collect.get(0));
                }
            });

        });
        res.put("state", true);
        res.put("msg", "查询成功");
        res.put("data", taskPage1);
        return res;
    }

    @Override
    public Object getTaskDetail(Integer tid) {
        HashMap<String, Object> res = new HashMap<>();
        Task task = taskMapper.selectById(tid);
        if (task == null) {
            res.put("state", false);
            res.put("msg", "任务不存在！");
            return res;
        }
        res.put("state", true);
        res.put("msg", "查询成功");
        res.put("data", task);
        return res;
    }
}




